#!/usr/bin/env python
# coding:utf8

from basis.basis_page import BasisPage
from  config.elelocinfo import *



class BrowsePage(BasisPage):

    def first_sub_bar(self):
        driver = self.basis_driver
        def browse_first_page():
            self.log('用户浏览首页商品')
            driver.scrolldown_uniform()
            driver.scrolltop_uniform()
        browse_first_page()
        driver.sleep(2)
        self.log('导航栏')
        def _sub_bar():

            sub_list = driver.locate_elements(sub_bar)
            i = 1
            is_header = True
            for sub in sub_list:
                if is_header:
                    is_header = False
                    continue
                sub.click()
                driver.switch_to_window_by_handle(i)
                try:
                    sub_sort_list = driver.locate_elements(sub_sort)
                    for sort in sub_sort_list:
                        sort.click()
                        driver.scrolldown_uniform()
                        page_number = driver.count_elements(page_xpath)
                        for j in range(2,page_number-1):
                            driver.sleep(2)
                            driver.click(page_xpath1+str(j)+page_xpath2)
                            driver.sleep(2)
                            driver.scrolldown_uniform()
                            continue
                except:
                    self.log('无下一页')
                driver.sleep(2)
                driver.switch_to_window_by_handle(0)
                driver.sleep(3)
                i = i + 1

        _sub_bar()


    #商品分类
    def browse_first_sort(self):
        driver = self.basis_driver
        def qbfl():
            self.log('全部商品分类导航栏')

            sort1_list = driver.locate_elements(spfl_xpath)
            i = 1
            for sort in sort1_list:
                sort.click()
                self.log('通过句柄转换窗口')
                driver.switch_to_window_by_handle(i)
                driver.sleep(2)
                self.log('滑动滚动条')
                driver.scrolldown_uniform()
                try:
                    page_number = driver.count_elements(page_xpath)
                    for j in range(3,page_number):
                        driver.sleep(2)
                        driver.click(page_xpath1+str(j)+page_xpath2)
                        driver.sleep(2)
                        driver.scrolldown_uniform()
                except:
                    print('无下一页')
                driver.sleep(2)
                driver.switch_to_window_by_handle(0)
                driver.sleep(3)
                i = i + 1

        qbfl()
    #商品子分类
    def browse_sub_bar(self):
        driver = self.basis_driver
        def zbnf():
            self.log('正版奶粉子分类')
            driver.mouseover_to(zbnf_xpath)
            sort2_list = driver.locate_elements(zhengbannaifen_xpath)
            i = 1
            for sort in sort2_list:
                sort.click()
                self.log('通过句柄转换窗口')
                driver.switch_to_window_by_handle(i)
                driver.sleep(5)
                self.log('滑动滚动条')
                driver.scrolldown_uniform()
                try:
                    page_number = driver.count_elements(page_xpath)
                    for j in range(3, page_number):
                        driver.sleep(2)
                        driver.click(page_xpath1 + str(j) + page_xpath2)
                        driver.sleep(2)
                        driver.scrolldown_uniform()
                except:
                    print('无下一页')
                driver.sleep(2)
                driver.switch_to_window_by_handle(0)
                driver.close_browser()
                driver.sleep(3)
                i = i + 1

        zbnf()
        def myyp():
            self.log('母婴用品子分类')
            driver.mouseover_to(myyp_xpath)
            sort2_list = driver.locate_elements(muyingyongpin_xpath)
            i = 1
            for sort in sort2_list:
                sort.click()
                self.log('通过句柄转换窗口')
                driver.switch_to_window_by_handle(i)
                driver.sleep(5)
                self.log('滑动滚动条')
                driver.scrolldown_uniform()
                try:
                    page_number = driver.count_elements(page_xpath)
                    for j in range(3, page_number):
                        driver.sleep(2)
                        driver.click(page_xpath1 + str(j) + page_xpath2)
                        driver.sleep(2)
                        driver.scrolldown_uniform()

                except:
                    print('无下一页')

                driver.sleep(2)
                driver.switch_to_window_by_handle(0)
                driver.sleep(2)
                i = i + 1
        myyp()

        def mzgh():
            self.log('美妆个护子分类')
            driver.mouseover_to(mzgh_xpath)
            sort2_list = driver.locate_elements(meizhuanggeghu_xpath)
            i = 1
            for sort in sort2_list:
                sort.click()
                self.log('通过句柄转换窗口')
                driver.switch_to_window_by_handle(i)
                driver.sleep(5)
                self.log('滑动滚动条')
                driver.scrolldown_uniform()
                try:
                    page_number = driver.count_elements(page_xpath)
                    for j in range(3, page_number):
                        driver.sleep(2)
                        driver.click(page_xpath1 + str(j) + page_xpath2)
                        driver.sleep(2)
                        driver.scrolldown_uniform()

                except:
                    print('无下一页')
                driver.sleep(2)
                driver.switch_to_window_by_handle(0)
                driver.sleep(3)
                i = i + 1
        mzgh()

        def yybj():
            self.log('营养保健子分类')
            driver.mouseover_to(yybj_xpath)
            sort2_list = driver.locate_elements(yingyangbaojian)
            i = 1
            for sort in sort2_list:
                sort.click()
                self.log('通过句柄转换窗口')
                driver.switch_to_window_by_handle(i)
                driver.sleep(5)
                self.log('滑动滚动条')
                driver.scrolldown_uniform()
                try:
                    page_number = driver.count_elements(page_xpath)
                    for j in range(3, page_number):
                        driver.sleep(2)
                        driver.click(page_xpath1 + str(j) + page_xpath2)
                        driver.sleep(2)
                        driver.scrolldown_uniform()

                except:
                    print('无下一页')

                driver.sleep(2)
                driver.switch_to_window_by_handle(0)
                driver.sleep(3)
                i = i + 1
        yybj()

        def qt():
            self.log('其他子分类')
            driver.mouseover_to(qt_xpath)
            sort2_list = driver.locate_elements(qita_xpath)
            i = 1
            for sort in sort2_list:
                sort.click()
                self.log('通过句柄转换窗口')
                driver.switch_to_window_by_handle(i)
                driver.sleep(5)
                self.log('滑动滚动条')
                driver.scrolldown_uniform()
                try:
                    page_number = driver.count_elements(page_xpath)
                    for j in range(3, page_number):
                        driver.sleep(2)
                        driver.click(page_xpath1 + str(j) + page_xpath2)
                        driver.sleep(2)
                        driver.scrolldown_uniform()

                except:
                    print('无下一页')

                driver.sleep(2)
                driver.switch_to_window_by_handle(0)
                driver.sleep(3)
                i = i + 1
        qt()

    #国家地区管
    def browse_country(self):
        driver = self.basis_driver
        self.log('全部商品分类导航栏')
        driver.scrolldown_uniform()
        sort1_list = driver.locate_elements(country_xpath)
        i = 1
        for sort in sort1_list:
            sort.click()
            self.log('通过句柄转换窗口')
            driver.switch_to_window_by_handle(i)
            driver.sleep(5)
            self.log('滑动滚动条')
            driver.scrolldown_uniform()
            try:
                page_number = driver.count_elements(page_xpath)
                for j in range(3, page_number):
                    driver.sleep(2)
                    driver.click(page_xpath1 + str(j) + page_xpath2)
                    driver.sleep(2)
                    driver.scrolldown_uniform()

            except:
                print('无下一页')

            driver.sleep(2)
            driver.switch_to_window_by_handle(0)
            driver.sleep(3)
            i = i + 1

    #热门品牌
    def top_brand(self):
        driver = self.basis_driver
        self.log('全部商品分类导航栏')
        driver.scrolldown_uniform()
        sort1_list = driver.locate_elements(top_brand_xpath)
        i = 1
        for sort in sort1_list:
            sort.click()
            self.log('通过句柄转换窗口')
            driver.switch_to_window_by_handle(i)
            driver.sleep(5)
            self.log('滑动滚动条')
            driver.scrolldown_uniform()
            try:
                page_number = driver.count_elements(page_xpath)
                for j in range(3, page_number):
                    driver.sleep(2)
                    driver.click(page_xpath1 + str(j) + page_xpath2)
                    driver.sleep(2)
                    driver.scrolldown_uniform()

            except:
                print('无下一页')

            driver.sleep(2)
            driver.switch_to_window_by_handle(0)
            driver.sleep(3)
            i = i + 1
